package com.itzz.spring_zhoneHeng.service.Impl;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.BookDao;
import com.itzz.spring_zhoneHeng.dao.BookShelfDao;
import com.itzz.spring_zhoneHeng.dao.ClassificationDao;
import com.itzz.spring_zhoneHeng.dao.UserDao;
import com.itzz.spring_zhoneHeng.pojo.Book;
import com.itzz.spring_zhoneHeng.pojo.BookShelf;
import com.itzz.spring_zhoneHeng.pojo.Chapters;
import com.itzz.spring_zhoneHeng.pojo.Order;
import com.itzz.spring_zhoneHeng.service.BookService;
import com.itzz.spring_zhoneHeng.service.OrderService;
import com.itzz.spring_zhoneHeng.util.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    BookDao bookDao;

    @Autowired
    Upload uploadImg;

    @Autowired
    ClassificationDao classificationDao;

    @Autowired
    BookShelfDao bookShelfDao;

    @Autowired
    OrderService orderService;


    @Override
    //查询所有的书籍
    public RestResponse queryAllBook(Book book, Integer pageNum, Integer pageSize){
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //查询所有
        List<Book> books = bookDao.queryAllBook(book);
        //添加到分页插件中
        PageInfo<Book> chaptersPageInfo = new PageInfo<>(books);
        return RestResponse.ok(chaptersPageInfo);
    }

    //根据id回显书籍的信息
    @Override
    public RestResponse queryById(Integer id){
        Book book = bookDao.queryById(id);
        return RestResponse.ok(book);
    }

    //修改书籍(1)
    @Override
    @Transactional
    public RestResponse updateBook(Book book, MultipartFile files,Integer userId){
        String path = uploadImg.upLoad(files);
        if(path!=null){
            book.setPath(path);
        }

        int i = bookDao.updateBook(book);

        //首先根据bookId 查询b_bs表中的bsid
        //根据bookid查询List<Inter>  bsid
        List<Integer> bsids = bookDao.selectBsid(book.getId());
        for(Integer bsid:bsids){
            //如果为普通书籍
            if(book.getVip()==1){
                //修改书籍的时候需要添加
                BookShelf bookShelf = new BookShelf();
                bookShelf.setId(bsid);
                bookShelf.setBookName(book.getBookName());
                bookShelf.setAuthor(book.getAuthor());
                bookShelf.setIntroduce(book.getIntroduce());
//            bookShelf.setCategory(book.getCategory());
                bookShelf.setPath(book.getPath());
                bookShelf.setVip(book.getVip());
                //修改书架的信息
                bookShelfDao.updateBookShelf(bookShelf);
            }else{
                //根据id删除书架对应的书籍
                bookShelfDao.deleteBookShelf(bsid);
                //删除书架对应的关联表
                //根据id删除书架和书籍的关联表
                bookShelfDao.deleteBookShelfAndBook(bsid);
                //根据bsid删除用户和书架的关系表(u_bs)b_bs
                bookShelfDao.delU_BS(bsid);

                //添加到订单表
//                Order order,Integer bid,Integer userId
                Order order = new Order();
                order.setBookName(book.getBookName());
                order.setAuthor(book.getAuthor());
                order.setPath(book.getPath());
                orderService.addOrder(order,book.getId(),userId);

//            bookShelf.setCategory(book.getCategory());
            }

        }


        if(i>0){
            return RestResponse.ok();
        }else{
            return RestResponse.fail(1,"修改失败");
        }
    }


    //添加书籍
    @Override
    @Transactional
    public RestResponse addBook(Book book, MultipartFile files,Integer classId){
        String path = uploadImg.upLoad(files);
        if(path!=null){
            book.setPath(path);
        }
        //添加书籍 (自己写的)
        int i = bookDao.addBook(book);
        //添加类别和书籍表  b_c
        int b_c = classificationDao.addB_C(book.getId(), classId);
        if(i>0&&b_c>0){
            return RestResponse.ok(i);
        }else{
            return RestResponse.fail(i,"添加失败");
        }
    }


    //删除书籍
    @Override
    @Transactional
    public RestResponse deleteById(Integer id){
        //根据bookid 查询b_bs 表中的数组bsid
        List<Integer> bsids = bookDao.selectBsid(id);
        //如果数组bsid不等于空或者.size()>0   删除对应的书架表
        if(bsids.size()>0&&bsids!=null&&!bsids.equals("")){
            for(Integer bsid:bsids){
                //根据bookid  查询关联表  o_b 中的 oid
                List<Integer> oids = bookShelfDao.queryOid(id);
                for(Integer oid:oids){
                    //删除用户和订单的关联表
                    orderService.delOrder(oid);
                }
                //删除书架表
                bookShelfDao.deleteBookShelf(bsid);
                //删除书架和书籍的关联表
                bookShelfDao.deleteBookShelfAndBook(bsid);
                //删除用户和书架的关联表
                bookShelfDao.delU_BS(bsid);



            }
        }
        int i = bookDao.deleteById(id);
        //删除book 关联的表B_C  根据bid删除
        bookDao.delB_C(id);
        //删除book 关联的表b_bs  根据bookid删除
        bookDao.delB_BS(id);
        //删除b_chap 表  根据bookid删除
        bookDao.delB_CHAP(id);
        //删除书籍时,删除订单的关联表 根据bookid 删除o_b表
        bookDao.delO_B(id);





        if(i==0){
            return RestResponse.fail(0,"删除失败");
        }
        return RestResponse.ok(i);
    }


    //根据bookid  查询数组 chapids  根据chapids  查询表chapters
    //根据queryChapids
    @Override
    public RestResponse queryChapids(Integer bookid) {
        //根据bookid  查询数组 chapids  根据chapids  查询表chapters
        List<Integer> chapters = bookShelfDao.queryChapids(bookid);
        //根据chapters 查询表chapters 中的id
        Integer[] messids = chapters.toArray(new Integer[chapters.size()]);
        List<Chapters> chapters1 = bookDao.Chapters(messids);

        if(chapters1.size()>0){
            return  RestResponse.ok(chapters1);
        }

        return RestResponse.fail(0,"查询失败");


    }





}
